angular.controller(ꞌmain-loopꞌ)   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 14
rs 9.4285
nop 0
1
'use strict';
2
3
angular
4
  .module('game')
5
  .controller('main-loop', ['$scope',
6
    '$interval',
7
    '$timeout',
8
    'savegame',
9
    'state',
10
    'data',
11
    'util',
12
    'reaction',
13
    function($scope, $interval, $timeout, savegame, state, data, util, reaction) {
14
      $scope.state = state;
15
      $scope.data = data;
16
      $scope.util = util;
17
18
      let self = this;
19
20
      self.update = function() {
21
        state.reactions = [];
22
        state.update(state.player);
23
        reaction.processReactions(state.reactions, state.player);
24
      };
25
26
      self.updateLoop = function() {
27
        self.update();
28
        let speed = 1000;
29
        if(state.fasterTicks){
30
          speed = 1;
31
          state.player.offline--;
32
          if(state.player.offline <= 0){
33
            state.fasterTicks = 0;
34
          }
35
        }
36
        $timeout(self.updateLoop, speed);
37
      };
38
39
      self.startup = function() {
40
        savegame.load();
41
        let elapsed = Math.floor(Date.now()/1000)-state.player.last_login;
42
        let total = util.calculateValue(data.global_upgrades.offline_time.power.base,
43
            data.global_upgrades.offline_time.power,
44
            state.player.global_upgrades.offline_time);
45
        // lets limit the offline elapsed time
46
        state.player.offline = Math.min(total, state.player.offline+elapsed);
47
48
        state.loading = false;
49
        // trigger the game loop
50
        $timeout(self.updateLoop, 1000);
51
        $interval(savegame.save, 10000);
52
      };
53
54
      $timeout(self.startup);
55
    }
56
  ]);
57